/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.util.LinkedList;
import java.util.List;
import utils.Utils;

/**
 *
 * @author wpc
 */
public class PacketGenerator implements Comparable<PacketGenerator>{

    private int pathId;
    private double rate;
    private long left;
    private double time;
    public PacketGenerator(Path path, double rate, long left) {
        this(path.getId(),rate,left);
    }
    public PacketGenerator(int pathId, double rate, long left) {
        this.pathId = pathId;
        this.rate = 1/rate;
        this.left = left;
        this.time = 0;
        nextTime();
    }

    public double now(){
        return time;
    }

    public boolean isEmpty(){
        return left<=0;
    }

    private Packet nextPacket(){
        Packet packet = new Packet(pathId, time,Utils.length_random());
        nextTime();
        left--;
        return packet;
    }

    private void nextTime(){
        time += Utils.exp_random(rate);
    }

    public Packet next(){
        return nextPacket();
    }

    public List<Packet> next(double endTime){
        List<Packet> list = new LinkedList<Packet>();
        while((!isEmpty())&&(now()<endTime))
            list.add(next());
        return list;
    }

    public int compareTo(PacketGenerator o) {
        double onow = o.now();
        if(this.time<onow){
            return -1;
        }else if(this.time==onow)
            return 0;
        else
            return 1;
    }
    
}
